/*
 * @lc app=leetcode.cn id=43 lang=javascript
 *
 * [43] 字符串相乘
 */

// @lc code=start
/**
 * @param {string} num1
 * @param {string} num2
 * @return {string}
 */
var multiply = function(num1, num2) {
   num1=num1.split("").map((item)=>item|0)
   num2=num2.split("").map((item)=>item|0)
   let count=0
   let ans=[]
   for(let i=num1.length-1;i>=0;i--){
       let add=0
       subRes=""
       for(let j=num2.length-1;j>=0;j--){
        let temp=add+num1[i]*num2[j]
        add=(temp/10)|0
        subRes=(temp-add*10)+subRes
       }
       if(add){
           subRes=add+subRes
       }
       subRes=subRes+"0".repeat(count)
       ans.push(subRes)
       count++
   }
   const twoBigSum=function(a,b){
       let add=0
       let sum=''
       while(a.length!=b.length){
           if(a.length<b.length){
               a="0"+a
           }else{
               b="0"+b
           }
       }
       for(let i=a.length-1;i>=0;i--){
            let res=(a[i]|0)+(b[i]|0)+add
             add=(res/10)|0
            let num=res%10
            sum=num+sum
       }
       return add?add+sum:sum
   }
   let all=ans.reduce((a,b)=>twoBigSum(a,b))
   if(all==0)return "0"
   return all
};//可将第一步中的大数相加变成乘法
// @lc code=end

const twoBigSum=function(a,b){
    let add=0
    let sum=''
    while(a.length!=b.length){
        if(a.length<b.length){
            a="0"+a
        }else{
            b="0"+b
        }
    }
    console.log(a,b)
    for(let i=a.length-1;i>=0;i--){
         let res=(a[i]|0)+(b[i]|0)+add
          add=(res/10)|0
         let num=res%10
         sum=num+sum
    }
    return add?add+sum:sum
}
twoBigSum("12","12")